package com.zd.shopping;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.zd.shopping.app.MyApp;
import com.zd.shopping.entity.TabEntity;
import com.zd.shopping.fragment.DemoFragment;
import com.zd.shopping.fragment.IndexFragment;
import com.zwz.mall.base.BaseActivity;
import com.zwz.mall.base.MyPagerAdapter;
import com.zwz.mall.utils.BarHeightUtils;
import com.zwz.mall.utils.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;

public class MainActivity extends BaseActivity
{
    @BindView(R.id.tl_2)
    CommonTabLayout tableLayout;
    @BindView(R.id.vp_2)
    ViewPager mViewPager;
    private long mExitTime;
    public static boolean isForeground = false;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"工作台"};
    private int[] mIconUnselectIds = {R.mipmap.ic_launcher};
    private int[] mIconSelectIds = {R.mipmap.ic_launcher};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        ((MyApp) getApplication()).mIsStart = true;
        super.onCreate(savedInstanceState, persistentState);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApp myApp = (MyApp) getApplication();
        if (myApp != null) {
            myApp.mIsStart = false;
        }
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter()
    {

    }

    @Override
    public void initView()
    {
        mFragments.add(new IndexFragment ());
        for (int i = 0; i < mTitles.length; i++)
        {
            mTabEntities.add(new TabEntity (mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), mFragments, mTitles));
        setTablayout();
    }
    @SuppressLint("ClickableViewAccessibility")
    private void setTablayout()
    {
        tableLayout.setOnTabSelectListener(new OnTabSelectListener()
        {
            @Override
            public void onTabSelect(int position)
            {
                tableLayout.setCurrentTab(position);
            }

            @Override
            public void onTabReselect(int position)
            {

            }
        });
        tableLayout.setTabData(mTabEntities);
        tableLayout.setOnTabSelectListener(new OnTabSelectListener()
        {
            @Override
            public void onTabSelect(int position)
            {
                if (position == 1)
                {
                }
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position)
            {
            }
        });
        //viewPage左右滑动的效果true为不可滑动，false为滑动
        mViewPager.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                return false;
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {


            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }

            @Override
            public void onPageSelected(int position)
            {
                if (position == 1)
                {

                }
                tableLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });

        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(mFragments.size() - 1);//界面的预加载
    }
    @Override
    public void fillData()
    {

    }

    @Override
    public void setListener()
    {

    }
    @Override
    protected void onResume() {
        isForeground = true;

        super.onResume();

    }

    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtils.showShort("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
